package com.jiuyin.dianjing.ui.activity.message;


import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.HeadPhotoAdapter;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.TeamDetail;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.team.TeamGameRecordsFragment;

import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TEAMID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;

public class AddFriendsActivity extends BaseActivity {
    @BindView(R.id.grid_members)
    RecyclerView gridMembers;
    @BindView(R.id.grid_often_games)
    RecyclerView gridGames;

    private TeamDetail mTeamDetail;
    private HeadPhotoAdapter membersAdapter;
    private HeadPhotoAdapter gamesAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_add_friends;
    }

    @Override
    protected void initData() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.layout_fragment, TeamGameRecordsFragment.newInstance("ac67059b7a7f49bf814f162019d80263"))
                .commit();
        fetchDetailData();
    }

    private void fetchDetailData() {
        HashMap hashMap = new HashMap();
        hashMap.put(KEY_TEAMID, "ac67059b7a7f49bf814f162019d80263");
        hashMap.put(KEY_TOKEN, HelperApplication.getToken());
        JSONObject data = new JSONObject(hashMap);
        ApiResultCallback requestCallback = new DefaultApiResult() {

            @Override
            public void onSuccess(JsonObject data) {
                mTeamDetail = new Gson().fromJson(data, TeamDetail.class);
                initView();
            }
        };
        ServerApi.post(ApiEnum.TEAM_DETAIL, data, null, progress, d -> addDisposable(d), requestCallback);
    }

    @Override
    protected void initView() {
        if (mTeamDetail != null) {
            membersAdapter.setData(mTeamDetail.headerList);
            gamesAdapter.setData(mTeamDetail.gameList);
        } else {
            membersAdapter = new HeadPhotoAdapter(40f);
            gamesAdapter = new HeadPhotoAdapter(40f);
            gridMembers.setAdapter(membersAdapter);
            gridGames.setAdapter(gamesAdapter);
            gridMembers.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
            gridGames.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        }
    }

    @OnClick({R.id.iv_back, R.id.iv_right_more})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_right_more:
                // TODO: 2020/6/28 添加好友页更多
                break;
        }
    }
}